package net.helix.ui.panels;

import net.helix.element.Element;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JSlider;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JScrollPane;

import net.helix.engine.Engine;

public class PropertiesPane extends JPanel implements ComponentListener {
	private static final long serialVersionUID = 1L;
	
	private ActionListener listener;
	
	private Engine engine;
	
	private JPanel mainPanel;
	
	private JLabel typeL; // infoGroup
	private JLabel nameL;
	private JLabel idL;
	private JButton swapB;
	
	private JCheckBox billboardCB; // transformation group
	private JCheckBox globalRateCB;
	private JCheckBox lightsCB;
	private JCheckBox drawCB;
	private JTextField posRotScaTF[][];
	private JCheckBox visibleCB;
	private JComboBox blendCombo;
	private JSlider alphaS;
	private JSlider colorS;
	
	public PropertiesPane(Engine engine){
		super();
		
		listener = createActionListener();
		
		this.engine = engine;
		
		this.addComponentListener(this);
		
		this.setLayout(new BorderLayout());
		
		mainPanel = new JPanel();
		
		resized();
		
		JScrollPane scrollPane = new JScrollPane(mainPanel);
		
		this.add(scrollPane, BorderLayout.CENTER);
		
		mainPanel.add(createInfoGroup());
		mainPanel.add(createTransformationGroup());
		mainPanel.add(createSettingsGroup());
		mainPanel.add(createConstraintsGroup());
	}
	private JPanel createInfoGroup(){
		JPanel group = new JPanel();
		group.setBorder(BorderFactory.createTitledBorder("Info"));
		group.setLayout(new BoxLayout(group, BoxLayout.Y_AXIS));

		typeL = new JLabel("Clip");
		group.add(typeL);
		
		nameL = new JLabel("Sonic Run");
		group.add(nameL);
		
		idL = new JLabel("ID: 008");
		group.add(idL);
		
		group.add(Box.createRigidArea(new Dimension(0, 20)));
		
		swapB = new JButton("Swap");
		group.add(swapB);
		
		group.setMinimumSize(new Dimension(200, 220));
		group.setPreferredSize(new Dimension(200, 220));
		return group;
	}
	private JPanel createTransformationGroup(){
		//billboardCB.setBorder(BorderFactory.createEmptyBorder());
		//rightP.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		
		JPanel group = new JPanel();
		group.setBorder(BorderFactory.createTitledBorder("Transformation"));
		group.setLayout(new GridLayout(1, 2));
		
		JPanel leftP = new JPanel();
		leftP.setLayout(new BoxLayout(leftP, BoxLayout.Y_AXIS));
		
		JPanel topLeftP = new JPanel();
		topLeftP.setLayout(new BoxLayout(topLeftP, BoxLayout.Y_AXIS));
		
		billboardCB = new JCheckBox("Billboard");
		topLeftP.add(billboardCB);
		
		globalRateCB = new JCheckBox("Ignore Global Rate");
		topLeftP.add(globalRateCB);
		
		lightsCB = new JCheckBox("Use Lights");
		topLeftP.add(lightsCB);
		
		drawCB = new JCheckBox("Draw On Top");
		topLeftP.add(drawCB);

		topLeftP.setAlignmentX(Box.CENTER_ALIGNMENT);
		leftP.add(topLeftP);
		
		leftP.add(Box.createRigidArea(new Dimension(0, 15)));
		
		JLabel xAxis[] = new JLabel[3];
		xAxis[0] = new JLabel("x");
		xAxis[1] = new JLabel("y");
		xAxis[2] = new JLabel("z");
		JLabel yAxis[] = new JLabel[3];
		yAxis[0] = new JLabel("pos");
		yAxis[1] = new JLabel("rot");
		yAxis[2] = new JLabel("sca");
		
		JPanel bottomLeftP = new JPanel();
		bottomLeftP.setLayout(new GridLayout(4, 4));
		posRotScaTF = new JTextField[3][3];
		
		for (int row = 0; row < 4; row++){
			for (int col = 0; col < 4; col++){
				if (row == 0 && col == 0){
					bottomLeftP.add(new JLabel(""));
				}else if (row == 0){
					bottomLeftP.add(xAxis[col - 1]);
				}else if (col == 0){
					bottomLeftP.add(yAxis[row - 1]);
				}else{
					JTextField textField = new JTextField("0");
					posRotScaTF[row - 1][col - 1] = textField;
					bottomLeftP.add(textField);
				}
			}
		}
		bottomLeftP.setMaximumSize(new Dimension(160, 80));
		leftP.add(bottomLeftP);

		group.add(leftP);
		
		JPanel rightP = new JPanel();
		rightP.setLayout(new BoxLayout(rightP, BoxLayout.Y_AXIS));
		
		JPanel topRightP = new JPanel();
		topRightP.setLayout(new BoxLayout(topRightP, BoxLayout.Y_AXIS));
		
		visibleCB = new JCheckBox("Visible");
		visibleCB.setSelected(true);
		topRightP.add(visibleCB);
		
		topRightP.add(Box.createRigidArea(new Dimension(0, 10)));
		
		JLabel blendL = new JLabel("Blend Mode:");
		topRightP.add(blendL);
		
		String strings[] = {"None", "blend1", "blend2", "blend3", "blend4"};
		blendCombo = new JComboBox(strings);
		blendCombo.setMaximumSize(new Dimension(100, 20));
		blendCombo.setAlignmentX(Box.LEFT_ALIGNMENT);
		topRightP.add(blendCombo);
		
		topRightP.setAlignmentX(Box.CENTER_ALIGNMENT);
		rightP.add(topRightP);
		
		rightP.add(Box.createRigidArea(new Dimension(0, 15)));
		
		JPanel bottomRightP = new JPanel();
		bottomRightP.setLayout(new GridLayout(1, 2));
		
		JPanel alphaP = new JPanel();
		alphaP.setLayout(new BoxLayout(alphaP, BoxLayout.Y_AXIS));
		
		JLabel alphaL = new JLabel("Alpha");
		alphaL.setAlignmentX(Box.CENTER_ALIGNMENT);
		alphaP.add(alphaL);
		
		alphaS = new JSlider(JSlider.VERTICAL, 0, 100, 100);
		alphaS.setMajorTickSpacing(50);
		alphaS.setMinorTickSpacing(10);
		alphaS.setPaintTicks(true);
		alphaS.setPaintLabels(true);
		alphaP.add(alphaS);
		
		bottomRightP.add(alphaP);
		
		JPanel colorP = new JPanel();
		colorP.setLayout(new BoxLayout(colorP, BoxLayout.Y_AXIS));
		
		JLabel colorL = new JLabel("Color");
		colorL.setAlignmentX(Box.CENTER_ALIGNMENT);
		colorP.add(colorL);
		
		colorS = new JSlider(JSlider.VERTICAL, 0, 100, 100);
		colorS.setMajorTickSpacing(50);
		colorS.setMinorTickSpacing(10);
		colorS.setPaintTicks(true);
		colorS.setPaintLabels(true);
		colorP.add(colorS);
		
		bottomRightP.add(colorP);

		bottomRightP.setMaximumSize(new Dimension(160, 110));
		bottomRightP.setMinimumSize(new Dimension(160, 110));
		bottomRightP.setPreferredSize(new Dimension(160, 110));
		rightP.add(bottomRightP);
		
		group.add(rightP);
		
		group.setMinimumSize(new Dimension(200, 220));
		group.setPreferredSize(new Dimension(200, 220));
		
		billboardCB.addActionListener(listener);
		globalRateCB.addActionListener(listener);
		lightsCB.addActionListener(listener);
		drawCB.addActionListener(listener);
		visibleCB.addActionListener(listener);
		
		return group;
	}
	private JPanel createSettingsGroup(){
		JPanel group = new JPanel();
		group.setBorder(BorderFactory.createTitledBorder("Settings"));
		group.setLayout(new GridLayout(1, 2));
		
		JPanel leftP = new JPanel();
		leftP.setLayout(new BoxLayout(leftP, BoxLayout.Y_AXIS));
		
		JLabel frameL = new JLabel("Frames: 4");
		leftP.add(frameL);
		
		leftP.add(Box.createRigidArea(new Dimension(0, 10)));
		
		JLabel onFrameL = new JLabel("On Frame:");
		leftP.add(onFrameL);
		
		JTextField onFrameTF = new JTextField("4");
		onFrameTF.setAlignmentX(Box.LEFT_ALIGNMENT);
		onFrameTF.setMaximumSize(new Dimension(60, 20));
		leftP.add(onFrameTF);
		
		leftP.add(Box.createRigidArea(new Dimension(0, 10)));
		
		JLabel modeL = new JLabel("Anim Mode:");
		leftP.add(modeL);
		
		String strings[] = {"None", "Loop", "cake", "cookie", "blah"};
		JComboBox modeCombo = new JComboBox(strings);
		modeCombo.setMaximumSize(new Dimension(100, 20));
		modeCombo.setAlignmentX(Box.LEFT_ALIGNMENT);
		leftP.add(modeCombo);
		
		leftP.add(Box.createRigidArea(new Dimension(0, 10)));
		
		JCheckBox strideCB = new JCheckBox("Stride");
		leftP.add(strideCB);
		
		group.add(leftP);
		
		JPanel rightP = new JPanel();
		rightP.setLayout(new BoxLayout(rightP, BoxLayout.Y_AXIS));

		rightP.add(Box.createRigidArea(new Dimension(0, 20)));
		
		JLabel rateL = new JLabel("Animation Rate:");
		rightP.add(rateL);
		
		JTextField rateTF = new JTextField("1");
		rateTF.setAlignmentX(Box.LEFT_ALIGNMENT);
		rateTF.setMaximumSize(new Dimension(60, 20));
		rightP.add(rateTF);
		
		rightP.add(Box.createRigidArea(new Dimension(0, 10)));
		
		JCheckBox interpolateCB = new JCheckBox("Interpolate Frames");
		rightP.add(interpolateCB);
		
		group.add(rightP);
		
		group.setMinimumSize(new Dimension(200, 220));
		group.setPreferredSize(new Dimension(200, 220));
		return group;
	}
	private JPanel createConstraintsGroup(){
		JPanel group = new JPanel();
		group.setBorder(BorderFactory.createTitledBorder("Constraints"));
		group.setMinimumSize(new Dimension(200, 220));
		group.setPreferredSize(new Dimension(200, 220));
		return group;
	}
	
	public void resized(){
		if(this.getWidth() > this.getHeight()){
			mainPanel.setLayout(new GridLayout());
		}else{
			mainPanel.setLayout(new GridLayout(0, 1));
		}
	}
	
	public ActionListener createActionListener(){
		ActionListener listener = new ActionListener(){
			public void actionPerformed(ActionEvent actionEvent){
				AbstractButton abstractButton = (AbstractButton)actionEvent.getSource();
				if (abstractButton == billboardCB){
					actionBillboardCB();
				}else if (abstractButton == globalRateCB){
					actionGlobalRateCB();
				}else if (abstractButton == lightsCB){
					actionLightsCB();
				}else if (abstractButton == drawCB){
					actionDrawCB();
				}else if (abstractButton == visibleCB){
					actionVisibleCB();
				}
			}
		};
		return listener;
	}
	
	private void actionBillboardCB(){
		System.out.println("BillboardCB set to: " + billboardCB.isSelected());
	}
	private void actionGlobalRateCB(){
		System.out.println("GlobalRateCB set to: " + globalRateCB.isSelected());
	}
	private void actionLightsCB(){
		System.out.println("LightsCB set to: " + lightsCB.isSelected());
	}
	private void actionDrawCB(){
		System.out.println("DrawCB set to: " + drawCB.isSelected());
	}
	private void actionVisibleCB(){
		System.out.println("VisibleCB set to: " + visibleCB.isSelected());
	}
	
	/**
	 * Changes the properties information to that
	 * of the selected Element of Elements.
	 * 
	 * @param elements ArrayList of Elements. Null or size 0 for stage.
	 */
	public void changeElement(ArrayList<Element> elements){
		if (elements == null || elements.size() == 0){
			// do stuff for the stage
		}else{
			// do stuff for the element stage
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	@Override
	public void componentHidden(ComponentEvent arg0) {
		
	}
	@Override
	public void componentMoved(ComponentEvent arg0) {
		
	}
	@Override
	public void componentResized(ComponentEvent arg0) {
		resized();
	}
	@Override
	public void componentShown(ComponentEvent arg0) {
		
	}
}
